/**
 * 订单饼状图统计
 * 
 * @return {}
 */
Ext.chart.Chart.CHART_URL = __ctxPath + '/ext/resources/charts.swf';
var orderPieChartForm = function() {
	var orderPieChartPanel = new Ext.form.FormPanel({
				id : 'orderPieChartForm',
				frame : true,
				bodyBorder : false,
				anchor : '100% 10%',
				labelAlign : 'right',
				items : [{
					layout : 'column',
					border : false,
					items : [{
						columnWidth : .25,
						layout : 'form',
						border : false,
						items : [{
							xtype : "datefield",
							id : "orderPieChartStartDate",
							name : "orderPieStartDate",
							fieldLabel : "开始日期",
							format : "Y-m-d",// 日期的格式
							anchor : "100%",
							listeners : {
								'select' : function() {
									Ext.getCmp('orderPieChart_checkBoxDay')
											.reset();
									Ext.getCmp('orderPieChartEndDate')
											.setMinValue(this.getValue());
								}
							}
						}]
					}, {
						columnWidth : .25,
						layout : 'form',
						border : false,
						items : [{
							id : "orderPieChartEndDate",
							xtype : "datefield",
							name : "orderPieEndDate",
							fieldLabel : "结束日期",
							anchor : "100%",
							format : "Y-m-d",// 日期的格式
							listeners : {
								'select' : function() {
									Ext.getCmp('orderPieChart_checkBoxDay')
											.reset();
									Ext.getCmp('orderPieChartStartDate')
											.setMaxValue(this.getValue());
								}
							}
						}]
					}, {
						columnWidth : .25,
						layout : 'form',
						border : false,
						items : [{
							id : 'orderPieChart_checkBoxDay',
							xtype : 'radiogroup',
							labelStyle : "text-align:right;",
							height : 20,
							name : 'radioBoxDay',
							fieldLabel : '天数',
							hideLabels : false,
							items : [{
										xtype : 'radio',
										boxLabel : '30天',
										name : 'radioDay',
										inputValue : 30
									}, {
										xtype : 'radio',
										boxLabel : '90天',
										name : 'radioDay',
										inputValue : 90
									}, {
										xtype : 'radio',
										boxLabel : '180天',
										name : 'radioDay',
										inputValue : 180
									}],
							listeners : {
								change : function(obj, newValue, oldValue,
										eOpts) {// 这事件是当radiogroup的值发生改变时进入
									console.info(newValue);
									if (newValue != null && newValue != ''
											&& newValue != 'null') {
										var orderPieChartStartDate = Ext
												.getCmp('orderPieChartStartDate')
												.setValue('');
										var orderPieChartEndDate = Ext
												.getCmp('orderPieChartEndDate')
												.setValue('');
										queryorderPieChart();
									}

								}
							}
						}]
					}, {
						columnWidth : .25,
						layout : 'form',
						border : false,
						items : [{
							layout : 'column',
							border : false,
							items : [{
										text : '查询',
										xtype : 'button',
										width : 80,
										handler : function() {
											queryorderPieChart();
										},
										style : {
											marginLeft : "100px",
											marginRight : "10px"
										}
									}, {
										text : '重置',
										xtype : 'button',
										width : 80,
										handler : function() {
											Ext.getCmp('orderPieChartForm')
													.getForm().reset();
											// 对日期的初始设置
											Ext
													.getCmp('orderPieChartStartDate')
													.setMaxValue(new Date());
											Ext.getCmp('orderPieChartEndDate')
													.setMinValue("");
										}
									}]
						}]
					}]
				}]
			});
	return orderPieChartPanel;
}

/**
 * 图表
 * 
 * @return {}
 */
var getPanelView = function() {
	var westPieChart = new Ext.Panel({
				title : "订单类型统计图",
				width : '33%',
				region : "west",// 东
				collapsible : true,
				html : "<div id='westPieChartdiv'></div>",
				listeners : {
					"afterrender" : westPieChartHandler
				}
			});
	var centerPieChart = new Ext.Panel({
				title : "订单金额分析统计图",
				width : '33%',
				region : "center",// 中
				html : "<div id='centerPieChartdiv'></div>",
				listeners : {
					"afterrender" : centerPieChartHandler
				}
			});
	var eastPieChart = new Ext.Panel({
				title : "SKU销售统计图",
				width : '33%',
				region : "east",// 西
				collapsible : true,
				html : "<div id='eastPieChartdiv'></div>",
				listeners : {
					"afterrender" : eastPieChartHandler
				}
			});
	var northPieChart = new Ext.form.FormPanel({
				id : 'orderReportFormPanel',
				title : "销售报表数据",
				height : 100,
				region : "north",// 南
				collapsible : true,
				frame : true,
				bodyBorder : false,
				labelAlign : 'right',
				items : [{
							layout : 'column',
							border : false,
							items : [{
										columnWidth : .25,
										layout : 'form',
										border : false,
										items : [{
													xtype : 'displayfield',
													anchor : '100%',
													name : "orderSum",
													ctCls : 'zhaospaceCSS3',
													fieldLabel : '订单总单量'
												}]
									}, {
										columnWidth : .25,
										layout : 'form',
										border : false,
										items : [{
													xtype : 'displayfield',
													anchor : '100%',
													name : "goodsSum",
													ctCls : 'zhaospaceCSS3',
													fieldLabel : '商品总个数'
												}]
									}, {
										columnWidth : .25,
										layout : 'form',
										border : false,
										items : [{
													xtype : 'displayfield',
													anchor : '100%',
													name : "orderSumPrice",
													ctCls : 'zhaospaceCSS3',
													fieldLabel : '订单总额'
												}]
									}, {
										columnWidth : .25,
										layout : 'form',
										border : false,
										items : [{
													xtype : 'displayfield',
													anchor : '100%',
													name : "orderPayPrice",
													ctCls : 'zhaospaceCSS3',
													fieldLabel : '实收总额'
												}]
									}]
						}, {
							layout : 'column',
							border : false,
							items : [{
										columnWidth : .25,
										layout : 'form',
										border : false,
										items : [{
													xtype : 'displayfield',
													anchor : '100%',
													id : 'orderReportProfit',
													ctCls : 'zhaospaceCSS3',
													fieldLabel : '优惠金额'
												}]
									}]
						}],
				reader : new Ext.data.JsonReader({
							success : 'success',
							root : 'data'
						}, [{
									name : "orderSum"
								}, {
									name : "goodsSum"
								}, {
									name : "orderSumPrice"
								}, {
									name : "orderPayPrice"
								}]),
				listeners : {
					render : function() {
						orderReportsFormLoad();
					}
				}
			});

	// Viewport
	var orderViewport = new Ext.Panel({
				layout : "border",
				anchor : '100% 90%',
				items : [westPieChart, centerPieChart, eastPieChart,
						northPieChart]

			});
	return orderViewport;
}
/**
 * 查询
 */
function queryorderPieChart() {
	westPieChartHandler();
	centerPieChartHandler();
	eastPieChartHandler();
	orderReportsFormLoad();
}
/**
 * 订单类型统计分析
 */
function westPieChartHandler() {
	var orderPieChartForm = Ext.getCmp('orderPieChartForm');
	// 请求数据
	Ext.Ajax.request({
				url : __ctxPath
						+ "/statisticManager/selectCountOrderByOrderType",
				params : orderPieChartForm.getForm().getValues(),
				method : "post",
				success : function(response, config) {
					var jsonData = Ext.util.JSON.decode(response.responseText);
					if (jsonData.success) {
						var myChart = new FusionCharts({
									type : 'pie3d',
									width : "100%",
									height : "100%",
									renderAt : 'westPieChartdiv',
									dataFormat : 'json',
									dataSource : {
										chart : {
											caption : "订单类型单量统计",
											bgColor : "#ffffff",
											use3DLighting : 0,
											showPercentValues : 1,
											showPercentInTooltip : 0,
											"toolTipColor" : "#ffffff",
											"toolTipBorderThickness" : "0",
											"toolTipBgColor" : "#000000",
											"toolTipBgAlpha" : "80",
											"toolTipBorderRadius" : "2",
											"toolTipPadding" : "5",
											"showHoverEffect" : "1",
											"showLegend" : "1",
											"legendBgColor" : "#ffffff",
											"legendBorderAlpha" : "0",
											"legendShadow" : "0",
											"legendItemFontSize" : "10",
											"legendItemFontColor" : "#666666",
											"useDataPlotColorForLabels" : "1",
											"smartLineColor": "#FF0000"
										},
										data : jsonData.list
									}
								});
						myChart.render();
					} else {
						Ext.MessageBox.show({
									title : "操作信息",
									msg : "订单类型统计数据获取失败!",
									buttons : Ext.MessageBox.OK,
									icon : Ext.MessageBox.ERROR
								});
					}
				},
				failure : function(response, config) {
					Ext.MessageBox.show({
								title : "错误提示",
								msg : "服务器请求异常，请稍后再试!",
								buttons : Ext.MessageBox.OK,
								icon : Ext.MessageBox.ERROR
							});
				}
			});

}
/**
 * 订单总金额图表分析
 */
function centerPieChartHandler() {
	var orderPieChartForm = Ext.getCmp('orderPieChartForm');
	// 请求数据
	Ext.Ajax.request({
		url : __ctxPath + "/statisticManager/selectCountOrderAmountByOrderType",
		params : orderPieChartForm.getForm().getValues(),
		method : "post",
		success : function(response, config) {
			var jsonData = Ext.util.JSON.decode(response.responseText);
			if (jsonData.success) {

				var myChart = new FusionCharts({
							type : 'mscolumn3d',
							width : "100%",
							height : "100%",
							renderAt : 'centerPieChartdiv',
							dataFormat : 'json',
							dataSource : {
								"chart" : {
									"caption" : "订单金额统计",
									"xAxisName" : "订单类型",
									"yAxisName" : "金额(元)",
									"paletteColors" : "#0075c2,#1aaf5d,#f2c500",
									"bgColor" : "#ffffff",
									"showBorder" : "0",
									"showCanvasBorder" : "0",
									"usePlotGradientColor" : "0",
									"plotBorderAlpha" : "10",
									"legendBorderAlpha" : "0",
									"legendBgAlpha" : "0",
									"legendShadow" : "0",
									"showHoverEffect" : "1",
									"valueFontColor" : "#282828",
									"rotateValues" : "1",
									"placeValuesInside" : "1",
									"divlineColor" : "#999999",
									"divLineDashed" : "1",
									"divLineDashLen" : "1",
									"canvasBgColor" : "#ffffff",
									"captionFontSize" : "14",
									"subcaptionFontSize" : "14",
									"subcaptionFontBold" : "0"
								},
								"categories" : [{
											"category" : jsonData.list.category
										}],
								"dataset" : [{
											"seriesname" : "实收金额",
											"data" : jsonData.list.discount
										}, {
											"seriesname" : "应收金额",
											"data" : jsonData.list.total
										}]
							}
						});
				myChart.render();
			} else {
				Ext.MessageBox.show({
							title : "操作信息",
							msg : "订单类型统计数据获取失败!",
							buttons : Ext.MessageBox.OK,
							icon : Ext.MessageBox.ERROR
						});
			}
		},
		failure : function(response, config) {
			Ext.MessageBox.show({
						title : "错误提示",
						msg : "服务器请求异常，请稍后再试!",
						buttons : Ext.MessageBox.OK,
						icon : Ext.MessageBox.ERROR
					});
		}
	});
}
/**
 * sku图表统计
 */
function eastPieChartHandler() {
	var orderPieChartForm = Ext.getCmp('orderPieChartForm');
	// 请求数据
	Ext.Ajax.request({
				url : __ctxPath + "/statisticManager/selectCountOrderBySku",
				params : orderPieChartForm.getForm().getValues(),
				method : "post",
				success : function(response, config) {
					var jsonData = Ext.util.JSON.decode(response.responseText);
					if (jsonData.success) {
						var myChart = new FusionCharts({
									type : 'doughnut2d',
									width : "100%",
									height : "100%",
									renderAt : 'eastPieChartdiv',
									dataFormat : 'json',
									dataSource : {
										chart : {
											caption : "SKU统计",
											bgColor : "#ffffff",
											use3DLighting : 0,
											showPercentValues : 1,
											showPercentInTooltip : 0,
											"toolTipColor" : "#ffffff",
											"toolTipBorderThickness" : "0",
											"toolTipBgColor" : "#000000",
											"toolTipBgAlpha" : "80",
											"toolTipBorderRadius" : "2",
											"toolTipPadding" : "5",
											"showHoverEffect" : "1",
											"showLegend" : "1",
											"legendBgColor" : "#ffffff",
											"legendBorderAlpha" : "0",
											"legendShadow" : "0",
											"legendItemFontSize" : "10",
											"legendItemFontColor" : "#666666",
											"useDataPlotColorForLabels" : "1",
											"smartLineColor": "#FF0000"
										},
										data : jsonData.list
									}
								});
						myChart.render();
					} else {
						Ext.MessageBox.show({
									title : "操作信息",
									msg : "订单类型统计数据获取失败!",
									buttons : Ext.MessageBox.OK,
									icon : Ext.MessageBox.ERROR
								});
					}
				},
				failure : function(response, config) {
					Ext.MessageBox.show({
								title : "错误提示",
								msg : "服务器请求异常，请稍后再试!",
								buttons : Ext.MessageBox.OK,
								icon : Ext.MessageBox.ERROR
							});
				}
			});

}
/**
 * 订单销售报表统计
 */
function orderReportsFormLoad() {
	var orderPieChartForm = Ext.getCmp('orderPieChartForm');
	Ext.getCmp("orderReportFormPanel").getForm().load({
		deferredRender : false,
		timeout : 1800000,
		url : __ctxPath + "/statisticManager/selectOrderReportInfo",
		params : orderPieChartForm.getForm().getValues(),
		root : "data",
		waitTitle : "请稍候",
		waitMsg : "正在载入数据，请稍候......",
		success : function(response, options) {
			var data = options.result.data;
			var profit=Number((data.orderSumPrice-data.orderPayPrice).toFixed(2));
			Ext.getCmp("orderReportProfit").setValue(profit);
		},
		failure : function(response, options) {
			Ext.MessageBox.show({
						title : "错误提示",
						msg : "载入失败,请稍后再试!",
						buttons : Ext.MessageBox.OK,
						icon : Ext.MessageBox.ERROR
					});
		}
	});
};

Ext.getCmp("centerTabPanel").getActiveTab().add(orderPieChartForm(),
		getPanelView());
Ext.getCmp("centerTabPanel").getActiveTab().doLayout();